package com.ttctracker.util;

import java.util.ArrayList;
import java.util.HashMap;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.ttctracker.classes.Direction;
import com.ttctracker.classes.Route;
import com.ttctracker.classes.StopItem;

public class RouteStopsParseHandler extends DefaultHandler {
	
	private Route searchRoute;
	private Direction tempDirection;
	private StopItem tempStopItem;
	private ArrayList<Direction> directions;
	private HashMap<String, StopItem> stopItems;
	private boolean direction_state = false;
	//private ArrayList<String> stops_IB_OB_AR;
	
	public Route getSearchRoute() {
		return searchRoute;
	}

	/*@Override
	public void endDocument() throws SAXException {
		
		ArrayList<String> stopTags=new ArrayList<String>();
		String[] stopStrings = new String[stopItems.size()];
		stopTags.addAll(Arrays.asList(stopItems.keySet().toArray(stopStrings)));
		//ArrayList<String> diff=new ArrayList<String>();
		
		for (Direction direction : directions) {		
			stopTags.removeAll(direction.get_stops());
		}
		
		if (stopTags.size()>0) {
			Collections.sort(stopTags);
			directions.add(new Direction(stopTags, searchRoute.get_tag()+"_hidden", searchRoute.get_tag()+" In bound, Out bound, and Arrival stops",  searchRoute.get_tag()+"_hidden"));
		}
			//stops_IB_OB_AR.addAll(stopTags);
		
	}*/


	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (localName.equals("direction")) {
			directions.add(tempDirection);
			direction_state = false;
		} else if (localName.equals("route")) {
			searchRoute.set_directions(directions);
			searchRoute.set_stopItems(stopItems);
		}
	}

	@Override
	public void startDocument() throws SAXException {
		searchRoute = new Route();
		directions = new ArrayList<Direction>();
		stopItems = new HashMap<String, StopItem>();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (localName.equals("route")) {
			searchRoute.set_tag(attributes.getValue("tag"));
			searchRoute.set_title(attributes.getValue("title"));
			searchRoute.set_minLon(Double.parseDouble(attributes
					.getValue("lonMin")));
			searchRoute.set_minLat(Double.parseDouble(attributes
					.getValue("latMin")));
			searchRoute.set_maxLon(Double.parseDouble(attributes
					.getValue("lonMax")));
			searchRoute.set_maxLat(Double.parseDouble(attributes
					.getValue("latMax")));

		} else if (localName.equals("stop")) {
			
			if (!direction_state) {
				String tag=attributes.getValue("tag");
				String title=attributes.getValue("title");
				/*String[] tag_splitStrings=tag.split("_");
				String tagString=tag_splitStrings.length>1?tag_splitStrings[1]:"";
				
				if (tagString.length()>0) {
					if (tagString.equals("ar")) {
						tagString=" (Arrival)";
					}else {
						tagString=tagString.equals("IB")?" (Inner Bound)":" (Outer Bound)";
					}					
				}*/
				
				tempStopItem = new StopItem();
				tempStopItem.set_tag(tag);
				tempStopItem.set_title(title);//+tagString);
				
				tempStopItem.set_lat(Double.parseDouble(attributes
						.getValue("lat")));
				tempStopItem.set_lon(Double.parseDouble(attributes
						.getValue("lon")));
				tempStopItem.set_stopID(attributes.getValue("stopId"));

				stopItems.put(tag, tempStopItem);
			} else {
				tempDirection.add_stop(attributes.getValue("tag"));
			}

		} else if (localName.equals("direction")) {
			direction_state = true;
			tempDirection = new Direction();
			tempDirection.set_tag(attributes.getValue("tag"));
			tempDirection.set_title(attributes.getValue("title"));
			tempDirection.set_name(attributes.getValue("name"));
		}
	}

}
